﻿// ===============================================================================
// Project Name        :    Gfw.Repositories.Account
// Project Description :    
// ===============================================================================
// Class Name          :    ActCompanyCashRepository
// Namespace:          :    Gfw.Repositories.Account
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 10:19:22
// Update Time         :    2012/6/24 10:19:22
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Repository;
using Gfw.Models.Account;
using NHibernate.Criterion;

namespace Gfw.Repositories.Account
{
    public class ActCompanyCashRepository : Repository<ActCompanyCash>
    {
        /// <summary>
        /// 根据提现用户ID与申请日期查询该用户提现记录
        /// </summary>
        /// <param name="userID">提现用户ID</param>
        /// <param name="applyDate">申请日期</param>
        /// <param name="unitOfWork"></param>
        /// <returns></returns>
        public ActCompanyCash GetCashByUserIDAndApplyDate(string userID, DateTime applyDate, UnitOfWork unitOfWork = null)
        {
            if (unitOfWork == null)
            {
                var query = Session.CreateCriteria("ActCompanyCash").Add(Expression.Eq("Userid", userID))
                    .Add(Expression.Eq("RequireDate", applyDate));

                return query.List<ActCompanyCash>().FirstOrDefault();
            }
            else
            {
                var query = unitOfWork.Session.CreateCriteria("ActCompanyCash").Add(Expression.Eq("Userid", userID))
                    .Add(Expression.Eq("RequireDate", applyDate));

                return query.List<ActCompanyCash>().FirstOrDefault();
            }
        }

        /// <summary>
        /// 根据状态信息查询符合条件的所有用户提现记录
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalPage"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="status">状态（Gfw.Infrastructure.Account.CashStatusEnum）</param>
        /// <returns></returns>
        public IList<ActCompanyCash> GetCashByStatus(string userID, int status, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int totalPage)
        {
            var query = Session.CreateCriteria("ActCompanyCash");

            if (!string.IsNullOrEmpty(userID))
                query = query.Add(Expression.Eq("Userid", userID));

            if (status != int.MaxValue) // 不是“全部”
                query = query.Add(Expression.Eq("Status", status));

            if (start.HasValue)
                query = query.Add(Expression.Ge("RequireDate", start.Value));

            if (end.HasValue)
            {
                end = end.Value.AddDays(1);
                query = query.Add(Expression.Le("RequireDate", end.Value));
            }

            query = query.AddOrder(new Order("RequireDate", true)); // 升序
            totalPage = (query.List().Count + pageSize - 1) / pageSize;

            return query.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize).List<ActCompanyCash>();
        }
    }
}
